package com.cowin.inf.service.impl;

import com.alibaba.fastjson.JSON;
import com.cowin.inf.dao.KyBdcStateMapper;
import com.cowin.inf.model.KyBdcState;
import com.cowin.inf.model.vo.KyBdcStateVo;
import com.cowin.inf.service.KyBdcStateService;
import com.cowin.base.AbstractServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spring.web.json.Json;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by  on 2019/09/24.
 *
 * @author CodeGenerator
 */
@Slf4j
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class KyBdcStateServiceImpl extends AbstractServiceImpl<KyBdcState> implements KyBdcStateService {

    @Resource
    private KyBdcStateMapper kyBdcStateMapper;

    @Override
    public List<KyBdcState> searchBdcState(KyBdcStateVo kyBdcStateVo) {
        return kyBdcStateMapper.searchBdcState(kyBdcStateVo);
    }

    @Override
    public void updateBdcState(KyBdcState kyBdcState) {
        log.info("修改BDC状态{}", JSON.toJSONString(kyBdcState));
        kyBdcState.setUpdateTime(new Date());
        Condition condition = new Condition(KyBdcState.class);
        condition.createCriteria().andCondition("CODE ='" + kyBdcState.getCode() + "' and NAME = '" + kyBdcState.getName() + "'");
        List<KyBdcState> kyBdcStates = kyBdcStateMapper.selectByCondition(condition);
        if (!CollectionUtils.isEmpty(kyBdcStates)) {
            kyBdcStateMapper.updateByCondition(kyBdcState, condition);
        } else {
            super.save(kyBdcState);
        }
    }


}
